/**
 * 认证路由
 */
const express = require('express');
const router = express.Router();
const {
    readUserAccounts,
    saveUserAccounts,
    readAdminAccounts,
    saveAdminAccounts
} = require('../services/accountService');
const { readConfig } = require('../services/configService');

/**
 * 验证用户登录
 * POST /api/auth/user-login
 */
router.post('/user-login', async (req, res) => {
    try {
        const { username, password } = req.body;
        
        const data = await readUserAccounts();
        const account = data.accounts.find(acc => acc.username === username && acc.password === password);
        
        if (account) {
            // 更新最后登录时间
            account.lastLogin = new Date().toISOString();
            await saveUserAccounts(data);
            
            res.json({
                success: true,
                message: '登录成功',
                data: {
                    id: account.id,
                    username: account.username,
                    name: account.name
                }
            });
        } else {
            res.status(401).json({
                success: false,
                error: '用户名或密码错误'
            });
        }
        
    } catch (error) {
        console.error('❌ 用户登录验证失败:', error);
        res.status(500).json({
            success: false,
            error: error.message
        });
    }
});

/**
 * 验证管理员登录
 * POST /api/auth/admin-login
 */
router.post('/admin-login', async (req, res) => {
    try {
        const { username, password } = req.body;
        
        const data = await readAdminAccounts();
        const account = data.accounts.find(acc => acc.username === username && acc.password === password);
        
        if (account) {
            // 更新最后登录时间
            account.lastLogin = new Date().toISOString();
            await saveAdminAccounts(data);
            
            res.json({
                success: true,
                message: '管理员登录成功',
                data: {
                    id: account.id,
                    username: account.username,
                    name: account.name,
                    role: account.role
                }
            });
        } else {
            res.status(401).json({
                success: false,
                error: '管理员用户名或密码错误'
            });
        }
        
    } catch (error) {
        console.error('❌ 管理员登录验证失败:', error);
        res.status(500).json({
            success: false,
            error: error.message
        });
    }
});

/**
 * 获取认证配置
 * GET /api/auth/config
 */
router.get('/config', async (req, res) => {
    try {
        const config = await readConfig();
        
        res.json({
            success: true,
            data: {
                enabled: config.auth?.enabled ?? true,
                sessionTimeout: config.auth?.sessionTimeout ?? 24
            }
        });
        
    } catch (error) {
        console.error('❌ 读取认证配置失败:', error);
        res.status(500).json({
            success: false,
            error: error.message
        });
    }
});

module.exports = router;

